﻿using System;
using System.ComponentModel;
using System.Linq;

namespace BaBaoFan.Modifier.Common
{
    public static class EnumExtension
    {
        public static string ToDescription(this Enum value)
        {
            try
            {
                var descAttr = (DescriptionAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
                var displayAttr = (DisplayNameAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DisplayNameAttribute), false);
                return descAttr.Any() ? descAttr[0].Description : displayAttr.Any() ? displayAttr[0].DisplayName : value.ToString();
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
}
